package com.kafka.demo.controller;

import com.kafka.demo.msg.ObjectRestResponse;
import com.kafka.demo.service.KafkaSender;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/kafka")
@Slf4j
public class CollectController {

    @Autowired
    private KafkaSender kafkaSender;



    @RequestMapping(value = "/send", method = RequestMethod.GET)
    public ObjectRestResponse sendKafka(HttpServletRequest request) {
        try {
            String message = request.getParameter("message");
            log.info("kafka的消息={}", message);
            kafkaSender.send( message);
            return new ObjectRestResponse<>().success().msg("发送kafka成功");
        } catch (Exception e) {
            return new ObjectRestResponse<>().failed().msg("发送kafka失败");
        }
    }
}